package com.ruoyi.car.controller;

import java.util.List;

import com.ruoyi.car.vo.UseAndCarListAndUserVO;
import com.ruoyi.car.vo.UseAndListAndUserAndDeptVO;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.car.domain.TmdCarUse;
import com.ruoyi.car.service.ITmdCarUseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用车申请Controller
 * 
 * @author ruoyi
 * @date 2024-04-23
 */
@Controller
@RequestMapping("/car/use")
public class TmdCarUseController extends BaseController
{
    private String prefix = "car/use";

    @Autowired
    private ITmdCarUseService tmdCarUseService;

    @RequiresPermissions("car:use:view")
    @GetMapping()
    public String use()
    {
        return prefix + "/use";
    }

    /**
     * 查询用车申请列表
     */
    @RequiresPermissions("car:use:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UseAndCarListAndUserVO tmdCarUse)
    {
        startPage();
        tmdCarUse.setNowName(ShiroUtils.getSysUser().getUserName());
        List<UseAndCarListAndUserVO> list = tmdCarUseService.selectTmdCarUseList(tmdCarUse);
        return getDataTable(list);
    }

    /**
     * 导出用车申请列表
     */
//    @RequiresPermissions("car:use:export")
//    @Log(title = "用车申请", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(TmdCarUse tmdCarUse)
//    {
//        List<TmdCarUse> list = tmdCarUseService.selectTmdCarUseList(tmdCarUse);
//        ExcelUtil<TmdCarUse> util = new ExcelUtil<TmdCarUse>(TmdCarUse.class);
//        return util.exportExcel(list, "用车申请数据");
//    }

    /**
     * 新增用车申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用车申请
     */
    @RequiresPermissions("car:use:add")
    @Log(title = "用车申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TmdCarUse tmdCarUse)
    {
        return toAjax(tmdCarUseService.insertTmdCarUse(tmdCarUse));
    }

    /**
     * 修改用车申请
     */
    @RequiresPermissions("car:use:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
//        TmdCarUse tmdCarUse = tmdCarUseService.selectTmdCarUseById(id);
        UseAndListAndUserAndDeptVO tmdCarUse = tmdCarUseService.selectTmdCarUseById(id);
        mmap.put("tmdCarUse", tmdCarUse);
        return prefix + "/edit";
    }

    /**
     * 修改保存用车申请
     */
    @RequiresPermissions("car:use:edit")
    @Log(title = "用车申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdCarUse tmdCarUse)
    {
        return toAjax(tmdCarUseService.updateTmdCarUse(tmdCarUse));
    }

    /**
     * 删除用车申请
     */
    @RequiresPermissions("car:use:remove")
    @Log(title = "用车申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tmdCarUseService.deleteTmdCarUseByIds(ids));
    }

    /**
     * 查看用车申请
     */
    @RequiresPermissions("car:use:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        UseAndListAndUserAndDeptVO tmdCarUse = tmdCarUseService.selectTmdCarUseById(id);
        mmap.put("tmdCarUse", tmdCarUse);
        return prefix + "/detail";
    }

    @RequiresPermissions("car:use:return")
    @PostMapping("/return")
    @ResponseBody
    public AjaxResult returnCar(@RequestParam("id") Long id)
    {
//        try {
            System.out.println(id);
            tmdCarUseService.returnCar(id);
            tmdCarUseService.updateStatus(id);
            return AjaxResult.success("车辆已归还");
//        } catch (Exception e) {
//            return AjaxResult.error("归还车辆失败: " + e.getMessage());
//        }
    }


}
